package com.wx.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author weixin 2024/11/20 16:10
 */
@Configuration
public class RedissonConfiguration {

    /**
     * Spring统一创建 RedissonClient，通过本项目RedissonUtils获取
     *
     * @return RedissonClient
     */
    @Bean
    public RedissonClient redissonCreate(Environment evn) {

        String host = evn.getProperty("spring.data.redis.host");
        String port = evn.getProperty("spring.data.redis.port");
        String password = evn.getProperty("spring.data.redis.password");
        String database = evn.getProperty("spring.data.redis.database");
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password)
                .setDatabase(Integer.parseInt(database));
        config.setCodec(new JsonJacksonCodec());
        return Redisson.create(config);
    }
}
